package org.example.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.annotation.CheckPermission;
import org.example.context.BaseContext;
import org.example.pojo.domain.Exhibition;
import org.example.pojo.domain.Reservation;
import org.example.pojo.dto.UpdateReservationStatusDto;
import org.example.pojo.vo.ReservationVo;
import org.example.pojo.vo.ReservationWithFeedbackVo;
import org.example.result.Result;
import org.example.service.ExhibitionService;
import org.example.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/reservations")
public class ReservationController {

    @Autowired
    private ReservationService reservationService;
    @Autowired
    private ExhibitionService exhibitionService;


    /** 分页获取当前用户所有预约记录，附带展览详情（visitor） **/
    @CheckPermission({"visitor"})
    @GetMapping("/user")
    public Result listReservations(@RequestParam(defaultValue = "1") int page,
                                   @RequestParam(defaultValue = "10") int size) {
        int userId = Integer.parseInt(BaseContext.getCurrentId());
        PageInfo<ReservationVo> voPage = reservationService.listReservationsByUser(userId, page, size);
        return Result.success("用户(" + userId + ")所有预约记录分页", voPage);
    }

    /** 查询所有可以预约的展览（visitor） **/
    @CheckPermission({"visitor"})
    @GetMapping("/available-exhibitions")
    public Result getAvailableExhibitions() {
        List<Exhibition> list = exhibitionService.getAvailableExhibitions();
        return Result.success("可预约展览列表", list);
    }

    /** 获取预约详情（visitor） **/
    @CheckPermission({"visitor"})
    @GetMapping("/{reservationId}")
    public Result getReservationDetails(@PathVariable Integer reservationId) {
        Reservation reservation = reservationService.getReservationDetails(reservationId);
        return Result.success("预约id("+reservationId+")的详情",reservation);
    }

    /** 查询用户所有的预约记录，根据状态过滤（visitor） **/
    @CheckPermission({"visitor"})
    @GetMapping("/user/status")
    public Result listReservationsByStatus(@RequestParam String status,
                                           @RequestParam(defaultValue = "1") int page,
                                           @RequestParam(defaultValue = "10") int size) {
        int userId = Integer.parseInt(BaseContext.getCurrentId());
        if (status.equals("completed")){
            PageInfo<ReservationWithFeedbackVo> pageInfo = reservationService.listCompletedWithFeedback(userId, status, page, size);
            return Result.success("用户所有状态为 " + status + " 的预约记录", pageInfo);
        }else{
            PageInfo<ReservationVo> pageInfo = reservationService.listReservationsByStatus(userId, status, page, size);
            return Result.success("用户所有状态为 " + status + " 的预约记录", pageInfo);
        }
    }

    /** 创建新预约（visitor） **/
    @CheckPermission({"visitor"})
    @PostMapping("/make")
    public Result makeReservation(@RequestBody Reservation reservation) {
            try{
                reservationService.makeReservation(reservation);
            }catch (RuntimeException ex){
                return Result.error("预约失败,"+ex.getMessage());
            }
        return Result.success("预约成功！");
    }

    /** 取消预约（visitor） **/
    @CheckPermission({"visitor"})
    @DeleteMapping("/cancel/{reservationId}")
    public Result cancelReservation(@PathVariable Integer reservationId) {
        reservationService.cancelReservation(reservationId);
        return Result.success("预约已取消！");
    }

    /** 更新预约状态为已完成（visitor） **/
    @CheckPermission({"visitor"})
    @PostMapping("/mark-completed/{reservationId}")
    public Result markReservationAsCompleted(@PathVariable Integer reservationId) {
        reservationService.markReservationAsCompleted(reservationId);
        return Result.success("预约状态已更新为已完成");
    }


    /**
     * 查询某条预约是否已完成反馈
     * @param exhibitionId 展览ID
     * @return { "hasFeedback": true/false } 没用到
     */
    @CheckPermission({"visitor"})
    @GetMapping("/{exhibitionId}/feedback-status")
    public Result getFeedbackStatus(@PathVariable Integer exhibitionId) {
        boolean has = reservationService.hasFeedbackForReservation(exhibitionId);
        Map<String, Boolean> data = Collections.singletonMap("hasFeedback", has);
        return Result.success("反馈状态查询", data);
    }
}
